package com.designPattern.responsibilityChain;

/**
 * @Auther: zwg
 * @Date: 2019/2/20 14:20
 * @Description:
 */
public abstract class Support {
    private String name;
    private Support next;

    public Support(String name) {
        this.name = name;
    }

    public Support setNext(Support next) {
        this.next = next;
        return next;
    }

    public final void support(Integer trouble) {
        if(resolve(trouble)) {
            done(trouble);
        } else if (next != null) {
            next.support(trouble);
        } else {
            fail(trouble);
        }
    }

    public String toString() {              // 显示字符串
        return "[" + name + "]";
    }

    protected abstract boolean resolve(Integer trouble);

    protected void done(Integer trouble) {//处理成功
        System.out.println(trouble + " is resolved by " + this + ".");
    }

    protected void fail(Integer trouble) {  // 处理失败
        System.out.println(trouble + " cannot be resolved.");
    }
}
